/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servergui;

import DataObject.TableObject;
import java.io.File;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Backkom
 */
public class TableValues extends AbstractTableModel {

    protected String[] tbHeader = null;
    protected JPanel tableView;
    protected File dataSourceFile;
    protected Vector<TableObject> dataSource;
    Vector<Class> classes;
    public boolean isEditCell = true;
    public TableObject lastRow = null;

    public TableValues(Vector<TableObject> dataRows, String[] tableHeader) {
        dataSource = dataRows;
        tbHeader = tableHeader;
    }

    public TableValues(Vector<TableObject> dataRows, String[] tableHeader, JPanel table, Vector<Class> cls) {
        dataSource = dataRows;
        tbHeader = tableHeader;
        tableView = table;
        classes = cls;
    }

    public void addLine() {
        if (CheckNull()) {
            if (tableView instanceof DesignView && !((DesignView) tableView).isNew) {
            } else {
                Object[] obj = new Object[tbHeader.length];
                Class[] cls = new Class[tbHeader.length];
                for (int i = 0; i < obj.length; i++) {
                    obj[i] = null;
                    if (classes == null) {
                        cls[i] = String.class;
                    } else {
                        cls[i] = classes.get(i);
                    }
                }
                if (tableView instanceof DesignView && tbHeader.length > 1) {
                    cls[2] = Boolean.class;
                    obj[2] = false;
                }
                TableObject tbvl = new TableObject(obj.length);
                tbvl.setValues(obj, cls);
                boolean add = true;
                if (lastRow == null) {
                    add = true;
                    lastRow = tbvl;
                } else if (lastRow.hasNull()) {
                    add = false;
                } else { //lastRow not have null
                    if (tableView instanceof TableViewData) {
                        String str = ((TableViewData) tableView).AddRow(lastRow);
                        if (!str.isEmpty()) {
                            JOptionPane.showMessageDialog(tableView, str, "Create Fail", JOptionPane.ERROR_MESSAGE);
                            add = false;
                        } else {
                            add = true;
                            lastRow = tbvl;
                        }
                    } else {  //design View                  
                        add = true;
                        lastRow = tbvl;
                    }
                }
                if (add) {
                    dataSource.add(tbvl);
                    fireTableDataChanged();
                }
            }
        }
    }

    @Override
    public int getRowCount() {
        return getDataSource().size();
    }

    @Override
    public int getColumnCount() {
        return tbHeader.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = getDataSource().elementAt(rowIndex).getAttributeAt(columnIndex);
        Class cls = getColumnClass(columnIndex);
        if (value == null) {
            if (cls == String.class) {
                value = "";
            }
            if (cls == Integer.class) {
                value = ""; //NULL
            }
            if (cls == Boolean.class) {
                value = false;
            }
        }
        return value;
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        boolean update = true;
        Object old = dataSource.get(row).getAttributeAt(column);
        String oldVal = "";
        if (old != null) {
            oldVal = old.toString();
        }
        if (value == null || value.toString().length() == 0 || value.toString().equalsIgnoreCase("null")) {
            JOptionPane.showMessageDialog(tableView, "Data not allow set to empty or NULL", "Update Fail", JOptionPane.ERROR_MESSAGE);
            update = false;
        } else if (value.toString().equals(oldVal)) {
            update = false;
        } else if (column == 0 && tbHeader[0].equals("Column Name")) {
            update = checkDuplicateColumnName(value, column, false);
            if (!update) {
                String alert = "Column name cannot duplicated";
                JOptionPane.showMessageDialog(tableView, alert, "Update Fail", JOptionPane.ERROR_MESSAGE);
            }
        } else if (tableView instanceof TableViewData) {
            if (tbHeader[column].endsWith("*")) { //Primarykey
                update = checkDuplicateColumnName(value, column, true);
            }
            if (!update) {
                String alert = "Primary key cannnot duplicated";
                JOptionPane.showMessageDialog(tableView, alert, "Update Fail", JOptionPane.ERROR_MESSAGE);
            } else {
                TableObject dt = dataSource.elementAt(row);
                if (!dt.hasNull()) {
                    String str = ((TableViewData) tableView).SetData(dataSource.elementAt(row), tbHeader[column], value, tbHeader);
                    if (!str.isEmpty()) {
                        JOptionPane.showMessageDialog(tableView, str, "Update Fail", JOptionPane.ERROR_MESSAGE);
                        update = false;
                    }
                }
            }
        } else { //Design View                                    
            if (column == 2 && tbHeader[2].equalsIgnoreCase("Primary Key") && ((boolean) value)) {
                for (TableObject i : dataSource) {
                    i.setAttributeAt(2, false);
                }
            }
        }
        if (update) {
            dataSource.elementAt(row).setAttributeAt(column, value);
            fireTableDataChanged();
        }
    }

    @Override
    public String getColumnName(int column) {
        if (column < tbHeader.length) {
            return tbHeader[column];
        }
        return null;
    }

    @Override
    public Class getColumnClass(int column) {
        Class dataType = super.getColumnClass(column);
        if (!getDataSource().isEmpty() && column < tbHeader.length) {
            dataType = dataSource.elementAt(0).getDataTypeAt(column);
        }
        return dataType;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        if (tableView instanceof DesignView && column > 0) {
            if (!((DesignView) tableView).isNew) {
                return false;
            }
        }
        return isEditCell;
    }

    /**
     * @return the dataSource
     */
    public Vector<TableObject> getDataSource() {
        return dataSource;
    }

    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(Vector<TableObject> dataSource) {
        this.dataSource = dataSource;
    }

    public boolean CheckNull() {
        for (TableObject i : dataSource) {
            if (i.hasNull()) {
                return false;
            }
        }
        return true;
    }

    private boolean checkDuplicateColumnName(Object value, int Column, boolean equal) {
        for (TableObject i : dataSource) {
            Object val = i.getAttributeAt(Column);
            String cp = "";
            if (val != null) {
                cp = val.toString();
            }
            if (!equal && cp.equalsIgnoreCase(value.toString())) {
                return false;
            }
            if (equal && cp.equals(value.toString())) {
                return false;
            }
        }
        return true;
    }
}
